package com.conversationboard.controller.ipaddress;

import javax.servlet.http.HttpServletRequest;

/**
 * Class to abstract out getting IP Addresses. If we are using mod_proxy in Apache, then we must extract the IP address from a
 * request header, and not using the request.getRemoteAddr() method, which will return the IP address of the local Apache proxy
 * server.
 * 
 * @author Keith Watson
 *
 */

public class IPAddress {

	public static String getIPAddress(HttpServletRequest request) {

		String ipAddress = request.getHeader("X-Forwarded-For");

		if ((ipAddress == null) || (ipAddress.trim().equals(""))) {
			ipAddress = request.getRemoteAddr();
		} else {

			/* Possibly more than one IP address: if there is, we want the first one */

			if (ipAddress.contains(",")) {
				String[] parts = ipAddress.split(",");
				ipAddress = parts[parts.length - 1];
			}
		}

		return ipAddress;

	}

}
